package cn.tj.wenda.cloud.bas.rest;

import cn.tj.wenda.cloud.bas.entity.BasLoginInfo;
import cn.tj.wenda.cloud.bas.entity.BasPassword;
import cn.tj.wenda.cloud.client.FallbackProxy;
import cn.tj.wenda.cloud.entity.ResponseBean;
import feign.hystrix.FallbackFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/***
 * 系统用户管理
 * @author Wenda
 * @date 2018年9月21日
 */

@FeignClient(name="bas-core",path = "/api",fallbackFactory = BasUserRestFuse.class)
@RequestMapping(value = "/bas/user")
@Api(tags="系统用户管理")
public interface BasUserRest {
	
	@ApiOperation(value="取得所有用户一览",notes="")
    @ApiImplicitParams({            
    	@ApiImplicitParam(name="page",value="分页索引",required=false,dataType="Integer"),
    	@ApiImplicitParam(name="limit",value="返回记录数，为0时返回所有记录",required=false,dataType="Integer"),
    	@ApiImplicitParam(name="params",value="检索条件（JSON格式）",required=false,dataType="String")
    })
    @GetMapping("")
    public ResponseBean select(@RequestParam("page") int page, @RequestParam("limit") int limit,
                               @RequestParam("params") String params);
    
	/**
	 * 管理员重置密码用
	 * @param userIds
	 * @return
	 */
	@ApiOperation(value="管理员重置选定用户的密码",notes="")
    @ApiImplicitParams({            
    	@ApiImplicitParam(name="userIds",value="用户ID列表",required=false,dataType="List<String>"),
    })
    @PostMapping("/upd/resetpwd")
	public ResponseBean restPwd(@RequestBody List<String> userIds);
	
	/**
	 * 当前用户修改自己的密码
	 * @param basPassword
	 * @return
	 */
	@ApiOperation(value="当前用户修改自己的密码",notes="")
    @ApiImplicitParams({            
    	@ApiImplicitParam(name="basPassword",value="用户密码修改信息",required=false,dataType="BasPassword"),
    })
	@PostMapping("/upd/pwd")
	public ResponseBean updPwd(@RequestBody BasPassword basPassword);
	
	/**
	 * 校验用户登录
	 * @param basLoginInfo
	 * @return 校验成功后，返回token
	 */
	@ApiOperation(value="用户登录",notes="")
    @ApiImplicitParams({            
    	@ApiImplicitParam(name="basLoginInfo",value="用户登录信息",required=true,dataType="BasLoginInfo"),
    })
	@PostMapping("/login")
	public ResponseBean login(@RequestBody BasLoginInfo basLoginInfo);
}

/**
 * 中断工厂类
 */
@Component
class BasUserRestFuse implements FallbackFactory<BasUserRest> {

	private String microName = "bas-core";

	@Override
	public BasUserRest create(Throwable cause) {
		BasUserRest client = (BasUserRest) FallbackProxy.newInstance(microName,BasUserRest.class,cause);
		return client;
	}
}
